﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;

namespace WebDAVClientDemo
{
    class Program
    {
        static string address;
        static void Main(string[] args)
        {
            address = @"http://localhost:8081/WebDAVServer/";
            Console.WriteLine("connection to {0}", address);
            while (true)
            {
                Console.Write(">> ");
                string command = Console.ReadLine();
                string resource;
                string putedData;
                switch (command)
                {
                    case "connect":
                        Console.WriteLine(string.Format("service: {0}", address));
                        Console.Write("new service address: ");
                        string tmp = Console.ReadLine();
                        if (!string.IsNullOrEmpty(tmp))
                            address = tmp;
                        break;
                    case "help":
                        Console.WriteLine("availabel methods:\n get - gets resource by its identification,\n put - loads resource to the server,\n delete - deletes resource,\n propfind - gets all properties"); break;
                    case "exit":
                        return;
                    case "get":
                        try{
                        Console.Write("  resource URI: ");
                        resource = Console.ReadLine();
                        Console.Write("  save to: ");
                        putedData = Console.ReadLine();
                        RequestGET(resource, putedData);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine(exc.Message);
                        }
                        break;
                    case "delete":
                        try
                        {
                            Console.Write("  resource URI: ");
                            resource = Console.ReadLine();
                            RequestDELETE(resource);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine(exc.Message);
                        }
                        break;
                    case "propfind":
                        try
                        {
                            Console.Write("  resource URI: ");
                            resource = Console.ReadLine();
                            RequestPROPFIND(resource);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine(exc.Message);
                        }
                        break;
                    case "put":
                        try
                        {
                            Console.Write("  resource URI: ");
                            resource = Console.ReadLine();
                            Console.Write("  loaded data: ");
                            putedData = Console.ReadLine();
                            RequestPUT(resource, putedData);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine(exc.Message);
                        }
                        break;
                    default:
                        Console.WriteLine("this command doesn't exist\n use \"help\" for details");
                        break;
                }

            }
        }

        static void RequestDemo(string method, string file)
        {
            WebRequest reqPOST = System.Net.WebRequest.Create(@"http://217.197.0.153:8081/WebDAVServer/" + file);
            //WebRequest reqPOST = System.Net.WebRequest.Create(@"http://localhost:8080/WebDAVServer/" + file);
            reqPOST.Method = method; 
            reqPOST.Timeout = 120000; // Устанавливаем таймаут соединения
            reqPOST.ContentType = "application/x-www-form-urlencoded"; // указываем тип контента
            reqPOST.Headers.Add("Version", "0.0.0.1");
            //byte[] sentData = Encoding.UTF8.GetBytes("<R:title xmlns:R=\"http://www.example.com/ns/\"></R:title>");
            //reqPOST.ContentLength = sentData.Length;
            //Stream sendStream = reqPOST.GetRequestStream();
            //sendStream.Write(sentData, 0, sentData.Length);
            //sendStream.Close();


            WebResponse resp = reqPOST.GetResponse();
            System.IO.Stream stream = resp.GetResponseStream();
            StreamReader sr = new System.IO.StreamReader(stream);
            StreamWriter sw = new StreamWriter("data");
            sw.Write(sr.ReadToEnd());
            sw.Close();
            sr.Close();
            string s = sr.ReadToEnd();
            Console.WriteLine(s);
        }

        static void RequestGET(string file, string putedDataPath)
        {
            //WebRequest reqPOST = System.Net.WebRequest.Create(@"http://217.197.0.153:8081/WebDAVServer/" + file);
            WebRequest request = System.Net.WebRequest.Create(address + file);
            request.Method = "GET";
            request.Timeout = 120000; // Устанавливаем таймаут соединения
            request.ContentType = "application/x-www-form-urlencoded"; // указываем тип контента
            request.Headers.Add("Version", "0.0.0.1");

            WebResponse resp = request.GetResponse();
            System.IO.Stream stream = resp.GetResponseStream();
            MemoryStream ms = new MemoryStream();
            stream.CopyTo(ms);

            FileStream fs = new FileStream(putedDataPath, FileMode.OpenOrCreate);
            byte[] data = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(data, 0, data.Length);
            fs.Write(data, 0, data.Length);
            fs.Close();
        }

        static void RequestDELETE(string file)
        {
            //WebRequest reqPOST = System.Net.WebRequest.Create(@"http://217.197.0.153:8081/WebDAVServer/" + file);
            WebRequest reqPOST = System.Net.WebRequest.Create(address + file);
            reqPOST.Method = "DELETE";
            reqPOST.Timeout = 120000; // Устанавливаем таймаут соединения
            reqPOST.ContentType = "application/x-www-form-urlencoded"; // указываем тип контента
            reqPOST.Headers.Add("Version", "0.0.0.1");

            WebResponse resp = reqPOST.GetResponse();
        }

        static void RequestPROPFIND(string file)
        {
            //WebRequest reqPOST = System.Net.WebRequest.Create(@"http://217.197.0.153:8081/WebDAVServer/" + file);
            WebRequest reqPOST = System.Net.WebRequest.Create(address + file);
            reqPOST.Method = "PROPFIND";
            reqPOST.Timeout = 120000; // Устанавливаем таймаут соединения
            reqPOST.ContentType = "application/x-www-form-urlencoded"; // указываем тип контента
            reqPOST.Headers.Add("Version", "0.0.0.1");
            byte[] send = Encoding.UTF8.GetBytes("<?xml version=\"1.0\"?><propfind><allprop/></propfind>");

            reqPOST.ContentLength = send.Length;
            Stream sendStream = reqPOST.GetRequestStream();
            sendStream.Write(send, 0, send.Length);
            sendStream.Close();


            WebResponse resp = reqPOST.GetResponse();
            System.IO.Stream stream = resp.GetResponseStream();
            MemoryStream ms = new MemoryStream();


            StreamReader sr = new System.IO.StreamReader(stream);


            char[] ch = new char[resp.ContentLength];
            sr.Read(ch, 0, ch.Length);
            sr.Close();
            string tmp = new string(ch);

            Console.WriteLine(tmp);
        }

        static void RequestPUT(string file, string dataPath)
        {
            WebRequest reqPOST = System.Net.WebRequest.Create(address + file);
            reqPOST.Method = "PUT";
            reqPOST.Timeout = 120000; // Устанавливаем таймаут соединения
            reqPOST.ContentType = "application/x-www-form-urlencoded"; // указываем тип контента
            reqPOST.Headers.Add("Version", "0.0.0.1");
            FileStream fs = new FileStream(dataPath, FileMode.Open);
            byte[] send = new byte[fs.Length]; 
            fs.Read(send, 0, send.Length);
            fs.Close();

            reqPOST.ContentLength = send.Length;
            Stream sendStream = reqPOST.GetRequestStream();
            sendStream.Write(send, 0, send.Length);
            sendStream.Close();

        }
    }
}
